<?php
/**
 */

Yii::import('backend.modules.user.components.Compare');
class YumProfile extends YumActiveRecord
{
	const PRIVACY_PRIVATE = 'private';
	const PRIVACY_PUBLIC = 'public';

	/**
	 * @var array of YumProfileFields
	 */
	static $fields=null;

	public function init()
	{
		parent::init();
		// load profile fields only once
		$this->loadProfileFields();
	}

	public function behaviors()  {
		return array_merge(parent::behaviors(), array(
					'Compare' => array(
						'class' => 'Compare')));
	}
	public function beforeSave() {
		$user_id = $this->user_id;		
//		if($user_id == '0' || is_null($user_id) || empty($user_id)){
//			Yii::log( Yum::t( 'Save profile error: userID empty, controller: {controller}, action: {action}', 
//								array('{controller}' => Yii::app()->controller->id, '{action}' => Yii::app()->controller->action->id)
//							)
//					);
//			
//		}
		return parent::beforeSave();		
	}
	public function afterSave() {
		$user_id = $this->user_id;		
//		if($user_id == '0' || is_null($user_id) || empty($user_id)){
//			Yii::log( Yum::t( 'Save profile error: userID empty, controller: {controller}, action: {action}, profileID:  {profileID}', 
//								array('{controller}' => Yii::app()->controller->id, '{action}' => Yii::app()->controller->action->id, '{profileID}' => $this->id)
//							)
//					);
//		}
		if($this->isNewRecord) 
			Yii::log( Yum::t( 'A profile been created: {profile}', array(
							'{profile}' => json_encode($this->attributes))));

		return parent::afterSave();
	}

	public function recentComments($count = 3) {
		$criteria = new CDbCriteria;
		$criteria->condition = 'id = ' .$this->id;
		$criteria->order = 'createtime DESC';
		$criteria->limit = $count;
		return YumProfileComment::model()->findAll($criteria);
	}

	public function beforeValidate() {
		if($this->isNewRecord)
			$this->timestamp = time();
		return parent::beforeValidate();
	}

	/**
	 * @param string $className
	 * @return YumProfile
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	// All fields that the user has activated in his privacy settings will
	// be obtained and returned for the use in the profile view
	public function getPublicFields() {
		if(!Yum::module('profile')->enablePrivacySetting)
			return false;

		$fields = array();

		if($privacy = @YumUser::model()->cache(500)->with('privacy')->findByPk($this->user_id)->privacy->public_profile_fields) {
			$i = 1;
			foreach(YumProfileField::model()->cache(500)->findAll() as $field) {
				if($i & $privacy && $field->visible != 0)
					$fields[] = $field;
				$i*=2;
			}
		}

		return $fields;
	}

	/**
	 * Returns resolved table name (incl. table prefix when it is set in db configuration)
	 * Following algorith of searching valid table name is implemented:
	 *  - try to find out table name stored in currently used module
	 *  - if not found try to get table name from UserModule configuration
	 *  - if not found user default {{profiles}} table name
	 * @return string
	 */
	public function tableName()
	{
		if (isset(Yum::module('profile')->profileTable))
			$this->_tableName = Yum::module('profile')->profileTable;
		else
			$this->_tableName = '{{profiles}}'; // fallback if nothing is set

		return Yum::resolveTableName($this->_tableName,$this->getDbConnection());
	}

	public function rules()
	{
		$required = array();
		$numerical = array();
		$rules = array();
		$safe = array();

		foreach (self::$fields as $field)
		{
			$field_rule = array();

			if ($field->required == 1)
				array_push($required, $field->varname);

			if ($field->field_type == 'int'
					|| $field->field_type == 'FLOAT'
					|| $field->field_type =='INTEGER'
					|| $field->field_type =='BOOLEAN')
				array_push($numerical,$field->varname);

			if ($field->field_type == 'DROPDOWNLIST')
				array_push($safe, $field->varname);

			if ($field->field_type =='VARCHAR'
					||$field->field_type=='TEXT')
			{
				$field_rule = array($field->varname,
						'length',
						'max'=>$field->field_size,
						'min' => $field->field_size_min);

				if ($field->error_message)
					$field_rule['message'] = Yum::t($field->error_message);
				array_push($rules,$field_rule);
			}

			if ($field->field_type=='DATE')
			{
				$field_rule = array($field->varname,
						'type',
						'type' => 'date',
						'dateFormat' => 'yyyy-mm-dd');

				if ($field->error_message)
					$field_rule['message'] = Yum::t( $field->error_message);
				array_push($rules,$field_rule);
			}

			if ($field->match)
			{
				$field_rule = array($field->varname,
						'match',
						'pattern' => $field->match);

				if ($field->error_message)
					$field_rule['message'] = Yum::t( $field->error_message);
				array_push($rules,$field_rule);
			}
			if ($field->range)
			{
				// allow using commas and semicolons
				$range=explode(';',$field->range);
				if(count($range)===1)
					$range=explode(',',$field->range);
				$field_rule = array($field->varname,'in','range' => $range);

				if ($field->error_message)
					$field_rule['message'] = Yum::t( $field->error_message);
				array_push($rules,$field_rule);
			}

			if ($field->other_validator)
			{
				$field_rule = array($field->varname,
						$field->other_validator);

				if ($field->error_message)
					$field_rule['message'] = Yum::t( $field->error_message);
				array_push($rules, $field_rule);
			}

		}

		array_push($rules,
				array(implode(',',$required), 'required'));
		array_push($rules,
				array(implode(',',$numerical), 'numerical', 'integerOnly'=>true));
		array_push($rules,
				array(implode(',',$safe), 'safe'));

		$rules[] = array('allow_comments, show_friends', 'numerical');
		$rules[] = array('email', 'unique', 'message' => 'Email đã tồn tại. Vui lòng nhập email khác');
		$rules[] = array('cmnd', 'unique', 'message' => 'CMND đã tồn tại. Vui lòng kiểm tra lại.');
		$rules[] = array('email', 'CEmailValidator');
		
		//$rules[] = array('cellphone1', 'match', 'pattern'=>'/^[0-9]{4}[0-9]{3}[0-9]{3,4}$/i');
		//$rules[] = array('cellphone2', 'match', 'pattern'=>'/^[0-9]{4}[0-9]{3}[0-9]{3,4}$/i');
		//$rules[] = array('cellphone3', 'match', 'pattern'=>'/^[0-9]{4}[0-9]{3}[0-9]{3,4}$/i');
		
		$rules[] = array('privacy', 'safe');

		return $rules;
	}

	public function relations()
	{
		$relations = array(
				'user' => array(self::BELONGS_TO, 'YumUser', 'user_id'),
				'comments' => array(self::HAS_MANY, 'YumProfileComment', 'profile_id'),
				'user_phonenumbers' => array(self::HAS_MANY, 'UserPhoneNumbers', 'userid'),
				);

		$fields = Yii::app()->db->cache(500)->createCommand(
				"select * from ".YumProfileField::model()->tableName()." where field_type = 'DROPDOWNLIST'")->queryAll();

		foreach($fields as $field) {
			$relations[ucfirst($field['varname'])] = array(
					self::BELONGS_TO, ucfirst($field['varname']), $field['varname']);

		}

		return $relations;
	}

	// Retrieve a list of all users that have commented my profile
	// Do not show my own profile visit
	public function getProfileCommentators() {
		$commentators = array();
		foreach($this->comments as $comment)
			if($comment->user_id != Yii::app()->user->id)
				$commentators[$comment->user_id] = $comment->user;

		return $commentators;
	}

	public function getProfileFields() {
		$fields = array();

		if(self::$fields)
			foreach(self::$fields as $field) {
				$varname = $field->varname;
				$fields[$varname] = Yum::t($varname);
			}
		return $fields;
	}


	public function attributeLabels()
	{
		$labels = array(
				'id' => Yum::t('Profile ID'),
				'user_id' => Yum::t('User ID'),
				'privacy' => Yum::t('Privacy'),
				'show_friends' => Yum::t('Show friends'),
				'allow_comments' => Yum::t('Allow profile comments'),
				);

		if(self::$fields)
			foreach (self::$fields as $field)
				$labels[$field->varname] = Yum::t($field->title);

		return $labels;
	}

	/**
	 * Load profile fields.
	 * Overwrite this method to get another set of fields
	 * @since 0.6
	 * @return array of YumProfileFields or empty array
	 */
	public function loadProfileFields()
	{
		if(self::$fields===null)
		{
			self::$fields=YumProfileField::model()->cache(500)->findAll();
			if(self::$fields==null)
				self::$fields=array();
		}
		return self::$fields;
	}
	
	public function getProfile($limit, $event_id, $day = 14){
		$criteria = new CDbCriteria();
		$criteria->alias = 'profile';
		//$criteria->select = '*';
		$criteria->with = array('user' => array('alias' => 'user')); //the same join
		//$criteria->join = 'LEFT JOIN `user` ON user.id = profile.user_id';
		$criteria->condition = '((lastvisit <= UNIX_TIMESTAMP() - '.$day.'*86400 AND user.lastvisit != 0) OR (createtime <= UNIX_TIMESTAMP() - '.$day.'*86400 AND lastvisit = 0)) 
								and user.id > (SELECT (CASE ISNULL(MAX(user_id)) WHEN 1 THEN 0 ELSE (SELECT user_id FROM send_email WHERE event_id='.$event_id.' ORDER BY user_id DESC LIMIT 0,1) END) FROM send_email WHERE event_id='.$event_id.' ORDER BY user_id DESC LIMIT 0,1)';
		$criteria->compare('receive_email', 1);
						
		$criteria->limit = $limit;
		$criteria->order = 'user.id';
		
		return $this->model()->findAll($criteria);
	}
	
	public function checkOpenId($openId) {
		$criteria = new CDbCriteria();
		$criteria->addSearchCondition('open_id', $openId);
		return $this->model()->find($criteria);
	}
	
	public function getProfilebyUsername($username){
		$sql = "SELECT * FROM user u LEFT JOIN profile p ON (u.id = p.user_id) WHERE u.username = '$username'";
		return $this->model()->findBySql($sql);
		
	}
	
	

}
